package chchanger;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author troffi
 */
public class ControlSysFs extends ControlAbstract{

	protected String fn="/sys/class/pvrusb2/unit-a/ctl_frequency/cur_val";

	@Override
	public void set(int freq) {
		frequency = freq;
		BufferedWriter out;
		try {
			out = new BufferedWriter(new FileWriter(fn));
			out.write(String.valueOf(frequency));
			out.close();
		} catch (IOException ex) {
			Logger.getLogger(ChChangerView.class.getName()).log(Level.SEVERE, null, ex);
		}

		System.out.println("set frequency > "+frequency);
		get();
	}

	public void set(){
		set(frequency);
	}

	@Override
	public int get() {
		try {
			RandomAccessFile f = new RandomAccessFile(fn, "r");
			String cf=f.readLine();
			f.close();
			System.out.println("get frequency < "+cf);
			frequency = Integer.valueOf(cf);
			return frequency;
		} catch (IOException ex) {
			Logger.getLogger(ChChangerView.class.getName()).log(Level.SEVERE, null, ex);
			return -1;
		}
	}

}
